// Modal.vue
<template>
    <BasicModal v-bind="$attrs" @register="register" :title="displayModuleTitle" @ok="onButtonClickConfirm"
        :body-style="bodyStyle" width="550px">
        <BasicForm @register="registerForm" style="height:200px" />
    </BasicModal>
</template>
<script lang="ts">
import { computed, defineComponent, ref, unref } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { BasicForm, useForm } from '/@/components/Form/index';
import { displayTitle } from '/@/utils/QcUnifyUtil';
import { QcUnifyTransToChildProps } from '/#/QcUnify';
import { useMessage } from '/@/hooks/web/useMessage';
import { updateStationDirectionsSchema } from './exchangeStation.data'
import { setStationDirection } from '/@/api/qms/exchangeChannel'
import { Input } from 'ant-design-vue';
export default defineComponent({
    components: { BasicModal, BasicForm, Input },
    name: 'UpdateDiretionModal',
    setup(_, { emit }) {
        const qcUnifyProps = ref<QcUnifyTransToChildProps>();
        const bodyStyle = {
            height: '230px',
        }
        // 当前操作的站点名
        const selectStcd = ref('');
        const { createMessage } = useMessage();
        const [
            registerForm,
            { setFieldsValue, resetFields, validate },
        ] = useForm({
            labelWidth: 80,
            schemas: updateStationDirectionsSchema,
            showActionButtonGroup: false,
            actionColOptions: {
                span: 22,
            },
        });
        const [register, { closeModal, setModalProps }] = useModalInner((data) => {
            resetFields();
            if (data != undefined && data != null) {
                let unrefData = unref(data);
                let param = {
                    id: null,
                    direction: [],
                    stcds: ['']
                };
                qcUnifyProps.value = unrefData;
                selectStcd.value = unrefData.moduleName;
                //接收父组件传递的数据
                if (unrefData?.dataId) {
                    param.id = unrefData.dataId
                }
                if (unrefData?.data) {
                    if (unrefData.data.direction == 3) {
                        param.direction = [1, 2]
                    } else {
                        param.direction = unrefData.data.direction == 0 ? [] : [unrefData.data.direction]
                    }
                    param.stcds = [unrefData.data.stcd];
                }
                setFieldsValue(param);

            }
        });
        /**
* 点击确认按钮
*/
        async function onButtonClickConfirm() {
            const values = await validate();
            let param = values;
            if (!param.direction || param.direction.length < 1) param.direction = 0
            else param.direction = param.direction.reduce((result, curValue) => {
                return result | curValue;
            })
            param.id = Number(param.id);
            setModalProps({ confirmLoading: true });
            try {
                //调用接口进行保存
                setStationDirection(param)
                    .then((res) => {
                        if (!res || Object.keys(res).length == 0) {
                            createMessage.success('保存成功。', 2);
                        }
                        closeModal();
                        resetFields();
                        emit('success');
                    })
                    .catch((ex) => {
                        console.log('catch ', ex);
                        createMessage.error(ex.msg);
                    });
            } finally {
                setModalProps({ confirmLoading: false });
            }
        }
        let displayModuleTitle = computed(() => {
            return displayTitle(qcUnifyProps.value)
        });
        return {
            register,
            selectStcd,
            bodyStyle,
            registerForm,
            closeModal,
            displayModuleTitle,
            onButtonClickConfirm
        };
    },
});
</script>